package com.a01_2021.thousandstimes.saas.service.impl;

import com.a01_2021.thousandstimes.exception.CustomException;
import com.a01_2021.thousandstimes.exception.CustomExceptionType;
import com.a01_2021.thousandstimes.saas.entity.UnionMember;
import com.a01_2021.thousandstimes.saas.mapper.UnionMemberMapper;
import com.a01_2021.thousandstimes.saas.service.SysInstanceService;
import com.a01_2021.thousandstimes.saas.service.UnionMemberService;
import com.a01_2021.thousandstimes.saas.service.UnionService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author FRQ
 * @since 2021-02-22
 */
@Service
public class UnionMemberServiceImpl extends ServiceImpl<UnionMemberMapper, UnionMember> implements UnionMemberService {

    @Resource
    UnionMemberMapper unionMemberMapper;

    @Resource
    UnionService unionService;

    @Resource
    SysInstanceService sysInstanceService;

    /**
     * 添加联盟成员
     *
     * @param unionId       联盟id
     * @param hostCompanyId 成员实例id
     * @throws CustomException 当联盟不存在，实例不存在时抛出该异常
     */
    @Override
    public void addMember(long unionId, long hostCompanyId) throws CustomException {
        if (!unionService.isUnion(unionId)) {
            throw new CustomException(CustomExceptionType.UNION_NOT_FOUND_ERROR, "找不到对应联盟");
        }
        if (!sysInstanceService.isInstance(hostCompanyId)) {
            throw new CustomException(CustomExceptionType.INSTANCE_NOT_FOUND_ERROR, "找不到对应实例");
        }
        UnionMember unionMember = new UnionMember();
        unionMember.setInstanceId(hostCompanyId);
        unionMember.setUnionId(unionId);
        unionMemberMapper.insert(unionMember);
    }
}
